﻿/*
 * RegExpress
 * 
 * Copyright (c) 2010, Daniel McGaughran
 * 
 * Licensed under the Apache Licence, Version 2.0 (the "Licence");
 * you may not use this file except in compliance with the Licence.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the Licence is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Licence for the specific language governing permissions and
 * limitations under the Licence.
 * 
 */

using System;
using System.IO;
using System.Reflection;
using RegExpressModel.Entities;


namespace RegExpressModel
{
	public class MainWindowModel
	{
		private Slide _currentSlide;
		private string _fullConfigPath;

		private SettingsModel _allSettings;
		private SlideDeck _slides;



		public MainWindowModel(int maxWidth, int maxHeight)
		{
			_slides = new SlideDeck();
			_allSettings = new SettingsModel(this);
			WindowSettings = new WindowSettings(maxWidth, maxHeight);
			SlideSettings = new SlideSettings();

			string basePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
			_fullConfigPath = Path.Combine(basePath, Constants.UserSettingsConfigFileName);
			_allSettings.RestoreStateFromFile(_fullConfigPath);

			string slideContentPath = Path.Combine(basePath, Constants.SlideDataFileName);
			SlideLoader loader = new SlideLoader()
			{
				AddSlide = _slides.AddSlide
			};
			loader.LoadSlideDataFromFile(slideContentPath);

			//Load slides
/*			_slides.AddSlide(new TitleSlide("title", "TitlePage")
			{
				Title = "Regular Expressions",
				SubTitle = "From Zero to Regex Hero",
				NextID = "xkcd"
			});
			_slides.AddSlide(new ImageSlide("xkcd", "ImagePage")
			{
				Title = "Regular Expressions",
				ImageUri = "/Resources/Media/xkcdRegex.png",
				NextID = "bmatch1",
				PrevID = "title"
			});
			_slides.AddSlide(new BasicMatchSlide("bmatch1", "BasicIsMatchPage")
			{
				Title = "Basic Validation",
				NextID = "mmatch1",
				PrevID = "xkcd",
				Caption = "The simplest regular expression:",
				MatchModel = new BasicMatchModel()
				{
					RegExpression = @"Go",
					Input = "Google"
				}
			});
			_slides.AddSlide(new MultiMatchSlide("mmatch1", "MultiIsMatchPage")
			{
				Title = "Basic Validation - Multiple Cases",
				PrevID = "bmatch1",
				NextID = "code1",
				Caption = "A few regular expression cases:",
				MatchModel = new MultiMatchModel()
				{
					RegExpression = @"Go",
					MatchCases = new List<SingleMatchCase>(new[]
					{
						new SingleMatchCase("Google"),
						new SingleMatchCase("Goddess"),
						new SingleMatchCase("Great")
					})
				}
			});
			_slides.AddSlide(new SourceCodeSlide("code1", "SourceCodePage")
			{
				Title = "Using .NET Regexes in Code",
				PrevID = "mmatch1",
				NextID = "fmatch1",
				Caption = "Declaring a shared Regex",
				SourceCode = @"class SomeMatcher
{
	private const string _tableExpression = @""table"";
	private static Regex _matcher;

	static SomeMatcher()
	{
		_matcher = new Regex(_expression, RegexOptions.Compiled | RegexOptions.CultureInvariant);
	}

	public bool HasMatch(string input)
	{
		return _matcher.IsMatch(input);
	}
}"
			});
			_slides.AddSlide(new FindMatchesSlide("fmatch1", "FindMatchesPage")
			{
				Title = "Find occurrences",
				PrevID = "code1",
				NextID = "rmatch1",
				Caption = "Finding all occurrences",
				MatchModel = new FindMatchesModel()
				{
					RegExpression = @"table",
					IsCaseIgnored = true,
					Input = @"<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd""><html xmlns=""http://www.w3.org/1999/xhtml"">
<head id=""ctl00_Head1""></head><body><form name=""aspnetForm"" method=""post"" action=""Default.aspx"" id=""aspnetForm"" 
onsubmit=""return popValues();""><div><input type=""hidden"" name=""__EVENTTARGET"" id=""__EVENTTARGET"" value="""" />
<input type=""hidden"" name=""__EVENTARGUMENT"" id=""__EVENTARGUMENT"" value="""" /></div><div>
<input type=""hidden"" name=""__EVENTVALIDATION"" id=""__EVENTVALIDATION"" value=""blah"" /></div>
<table cellpadding=""0"" cellspacing=""0"" border=""0"" width=""968"" align=""center"" style=""text-align: center;>
<tr><td><table style=""width: 968px;"" border=""0"" cellpadding=""0"" cellspacing=""0""><tr>
<td colspan=""3"" style=""vertical-align: top; text-align: left;"">
<table border=""0"" cellspacing=""0"" cellpadding=""0"" width=""100%""><tr><td align=""left"">
<a id=""ctl00_logo"" href=""./"" style=""border: 0""><img src=""images/common/logo.gif"" alt=""Tranz Scenic""
 style=""border-width:0px;"" /></a></td><td align=""right""><a id=""ctl00_TopMostImageContent_hlBanner"" href=""./""
 style=""border: 0""><img src=""images/common/banner.gif"" alt=""the official website for New Zealand train journeys""
 style=""border-width:0px;"" /></a></td></tr></table></td></tr><tr><td colspan=""3"">
 <a href=""#ctl00_TranscMenu_SkipLink""><img alt=""Skip Navigation Links""
 src=""/WebResource.axd?d=jjc9v3Lfa1e7ABbxPPwZFQ2&amp;t=633929533244218750"" width=""0"" height=""0""
 style=""border-width:0px;"" /></a><table id=""ctl00_TranscMenu"" class=""menu ctl00_TranscMenu_5 ctl00_TranscMenu_2"" 
 cellpadding=""0"" cellspacing=""0"" border=""0""><tr><td onmouseover=""Menu_HoverStatic(this)""
 onmouseout=""Menu_Unhover(this)"" onkeyup=""Menu_Key(event)"" id=""ctl00_TranscMenun0"">
 <table class=""menu ctl00_TranscMenu_4"" cellpadding=""0"" cellspacing=""0"" border=""0"" width=""100%""><tr>
 <td style=""white-space:nowrap;""><a class=""ctl00_TranscMenu_1 menu ctl00_TranscMenu_3""
 href=""services/tranzalpine.aspx"" style=""border-style:none;font-size:1em;"">tablet PCs</a></td></tr></table>
 </td><td style=""width:0px;""></td><td><img src=""images/common/menuseperator.gif"" alt="""" /></td>
 <td style=""width:0px;""></td><td onmouseover=""Menu_HoverStatic(this)"" onmouseout=""Menu_Unhover(this)""
 onkeyup=""Menu_Key(event)"" id=""ctl00_TranscMenun1""><table class=""menu ctl00_TranscMenu_4"" cellpadding=""0""
 cellspacing=""0"" border=""0"" width=""100%""><tr><td style=""white-space:nowrap;"">
 <a class=""ctl00_TranscMenu_1 menu ctl00_TranscMenu_3"" href=""services/constable.aspx""
 style=""border-style:none;font-size:1em;"">TranzCoastal</a></td></tr></table></td>"
				}
			});
			_slides.AddSlide(new ReplaceMatchesSlide("rmatch1", "ReplaceMatchesPage")
			{
				Title = "Replacement Example",
				PrevID = "fmatch1",
				Caption = "Removing HTML tags",
				MatchModel = new ReplaceMatchesModel()
				{
					RegExpression = @"<[^>]*>",
					ReplacementExpression = " ",
					IsCaseIgnored = true,
					Input = @"<!DOCTYPE html PUBLIC ""-//W3C//DTD XHTML 1.0 Transitional//EN"" ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd""><html xmlns=""http://www.w3.org/1999/xhtml"">
<head id=""ctl00_Head1""></head><body><form name=""aspnetForm"" method=""post"" action=""Default.aspx"" id=""aspnetForm"" 
onsubmit=""return popValues();""><div><input type=""hidden"" name=""__EVENTTARGET"" id=""__EVENTTARGET"" value="""" />
<input type=""hidden"" name=""__EVENTARGUMENT"" id=""__EVENTARGUMENT"" value="""" /></div><div>
<input type=""hidden"" name=""__EVENTVALIDATION"" id=""__EVENTVALIDATION"" value=""blah"" /></div>
<table cellpadding=""0"" cellspacing=""0"" border=""0"" width=""968"" align=""center"" style=""text-align: center;>
<tr><td><table style=""width: 968px;"" border=""0"" cellpadding=""0"" cellspacing=""0""><tr>
<td colspan=""3"" style=""vertical-align: top; text-align: left;"">
<table border=""0"" cellspacing=""0"" cellpadding=""0"" width=""100%""><tr><td align=""left"">
<a id=""ctl00_logo"" href=""./"" style=""border: 0""><img src=""images/common/logo.gif"" alt=""Tranz Scenic""
 style=""border-width:0px;"" /></a></td><td align=""right""><a id=""ctl00_TopMostImageContent_hlBanner"" href=""./""
 style=""border: 0""><img src=""images/common/banner.gif"" alt=""the official website for New Zealand train journeys""
 style=""border-width:0px;"" /></a></td></tr></table></td></tr><tr><td colspan=""3"">
 <a href=""#ctl00_TranscMenu_SkipLink""><img alt=""Skip Navigation Links""
 src=""/WebResource.axd?d=jjc9v3Lfa1e7ABbxPPwZFQ2&amp;t=633929533244218750"" width=""0"" height=""0""
 style=""border-width:0px;"" /></a><table id=""ctl00_TranscMenu"" class=""menu ctl00_TranscMenu_5 ctl00_TranscMenu_2"" 
 cellpadding=""0"" cellspacing=""0"" border=""0""><tr><td onmouseover=""Menu_HoverStatic(this)""
 onmouseout=""Menu_Unhover(this)"" onkeyup=""Menu_Key(event)"" id=""ctl00_TranscMenun0"">
 <table class=""menu ctl00_TranscMenu_4"" cellpadding=""0"" cellspacing=""0"" border=""0"" width=""100%""><tr>
 <td style=""white-space:nowrap;""><a class=""ctl00_TranscMenu_1 menu ctl00_TranscMenu_3""
 href=""services/tranzalpine.aspx"" style=""border-style:none;font-size:1em;"">tablet PCs</a></td></tr></table>
 </td><td style=""width:0px;""></td><td><img src=""images/common/menuseperator.gif"" alt="""" /></td>
 <td style=""width:0px;""></td><td onmouseover=""Menu_HoverStatic(this)"" onmouseout=""Menu_Unhover(this)""
 onkeyup=""Menu_Key(event)"" id=""ctl00_TranscMenun1""><table class=""menu ctl00_TranscMenu_4"" cellpadding=""0""
 cellspacing=""0"" border=""0"" width=""100%""><tr><td style=""white-space:nowrap;"">
 <a class=""ctl00_TranscMenu_1 menu ctl00_TranscMenu_3"" href=""services/constable.aspx""
 style=""border-style:none;font-size:1em;"">TranzCoastal</a></td></tr></table></td>"
				}
			});
*/
			if(!GoToSlide(SlideSettings.CurrentSlideName))
				CurrentSlide = _slides.FirstSlide;

		}



		public SettingsModel Settings
		{
			get { return _allSettings; }
		}


		public WindowSettings WindowSettings { get; private set; }
		public SlideSettings SlideSettings { get; private set; }


		public Slide CurrentSlide
		{
			get { return _currentSlide; }
			private set
			{
				_currentSlide = value;
				SlideSettings.CurrentSlideName = _currentSlide != null ? _currentSlide.ID : "";
			}
		}



		public bool HasNextSlide()
		{
			return !String.IsNullOrEmpty(CurrentSlide.NextID) && _slides.HasSlide(CurrentSlide.NextID);
		}

		public bool GoToNextSlide()
		{
			Slide nextSlide = _slides[CurrentSlide.NextID];

			if (nextSlide != null)
			{
				CurrentSlide = nextSlide;
				return true;
			}

			return false;
		}


		public bool HasPrevSlide()
		{
			return !String.IsNullOrEmpty(CurrentSlide.PrevID) && _slides.HasSlide(CurrentSlide.PrevID);
		}

		public bool GoToPrevSlide()
		{
			Slide prevSlide = _slides[CurrentSlide.PrevID];

			if (prevSlide != null)
			{
				CurrentSlide = prevSlide;
				return true;
			}

			return false;
		}


		public bool GoToFirstSlide()
		{
			Slide firstSlide = _slides.FirstSlide;

			if (firstSlide != null)
			{
				CurrentSlide = firstSlide;
				return true;
			}

			return false;
		}


		public bool GoToSlide(string slideID)
		{
			if(!_slides.HasSlide(slideID))
				return false;

			Slide targetSlide = _slides[slideID];

			if(targetSlide != null)
			{
				CurrentSlide = targetSlide;
				return true;
			}

			return false;
		}



		/// <summary>
		/// Saves all state to configuration file.
		/// </summary>
		/// <param name="currentWidth">Width of the current.</param>
		/// <param name="currentHeight">Height of the current.</param>
		/// <param name="isMaximised">if set to true [is maximised].</param>
		public void SaveState(int currentWidth, int currentHeight, bool isMaximised)
		{
			WindowSettings.Width = currentWidth;
			WindowSettings.Height = currentHeight;
			WindowSettings.IsMaximised = isMaximised;

			_allSettings.SaveStateToFile(_fullConfigPath);
		}

	
	}
}
